package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Category;
import com.ejuornal.dao.CategoryDao;

@Component("categoryDao")
public class CategoryDaoImpl extends DaoSupport<Category> implements
		CategoryDao {
	
	public void add(String sql, Object... objs) {
		super.add(sql, objs);
	}
	
	public void batchDelete(String sql, final int... ids) {
		super.batchDelete(sql, ids);
	}
	
	public void update(String sql, Object... objs) {
		super.update(sql, objs);
	}

	/**
	 * 描述:获取前count数据
	 */
	public List<Category> top(int count) {
		String sql = "select * from t_category limit 0,?";
		List<Category> list = super.getJdbcTemplate().query(sql,
				new Object[] { count }, new RowMapper<Category>() {
					public Category mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Category category = new Category();
						category.setEnName(rs.getString("enName"));
						category.setIntroduce(rs.getString("introduce"));
						category.setName(rs.getString("name"));
						return category;
					}
				});
		return list;
	}

	public List<Category> findAll() {
		String sql ="select * from t_category";
		List<Category> list = super.getJdbcTemplate().query(sql, new RowMapper<Category>(){
			public Category mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				Category categoryList = new Category();
				categoryList.setCategoryId(rs.getInt("categoryId"));
				categoryList.setEnName(rs.getString("enName"));
				categoryList.setIntroduce(rs.getString("introduce"));
				categoryList.setName(rs.getString("name"));
				return categoryList;
			}
		});
		return list;
	}
	
	public Category findById(int id) {
		String sql = "SELECT * FROM t_category WHERE categoryId = ?";
		Category category = super.getJdbcTemplate().queryForObject(sql, new Object[]{ id }, new RowMapper<Category>(){

			public Category mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				Category category = new Category();
				category.setCategoryId(rs.getInt("categoryId"));
				category.setName(rs.getString("name"));
				category.setEnName(rs.getString("enName"));
				category.setIntroduce(rs.getString("introduce"));
				
				return category;
			}
		});
		
		return category;
	}
}
